package extract.document.video;

import common.config.DbTableNames;
import common.config.IndexTypeNames;
import common.dao.config.ServerConfigurer;
import common.dao.couchdb.CouchDBDOCFactory;
import common.dao.elastic.ElasticSearchDao;
import common.dao.mysql.MySqlDao;
import extract.document.general.BaseIncreMode;
import extract.helper.CouchdbChangeHelper;
import extract.helper.DocUploadHelper;
import extract.helper.ElasticChangeHelper;
import extract.helper.TransactionHelper;
import org.ektorp.CouchDbConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Common on 11/27/16.
 */
public class VideoIncreMode {
    public static final Logger logger = LoggerFactory.getLogger(VideoIncreMode.class);

    public static class VideoThread implements Runnable{

        private Integer sleep_time = 120000;
        private boolean run_flag = true;
        private VideoIncreMode video_runner = new VideoIncreMode();

        public void run(){

            while (run_flag){
                logger.info("[VideoThread] ready to update video ----->>>>> \n\n");
                video_runner.run();
                logger.info("[VideoThread] no more video update! sleep a while!\n\n");
                TransactionHelper.GCAndSleep(sleep_time);

            }
        }
    }



    private BaseIncreMode incre_runner;

    public VideoIncreMode(){

        new ServerConfigurer().initElasticSearch_v2();

        CouchDBDOCFactory couch_factory = new CouchDBDOCFactory();

        CouchDbConnector video_con = couch_factory.getVideoCon();

        CouchdbChangeHelper video_helper = new CouchdbChangeHelper(video_con, new MySqlDao(),
                DbTableNames.DBType.DOC, DbTableNames.TBType.VIDEO);

        ElasticChangeHelper es_helper = new ElasticChangeHelper(new ElasticSearchDao(), IndexTypeNames.ESType.VIDEO);

        DocUploadHelper docUploadHelper = new DocUploadHelper( new MySqlDao() );

        this.incre_runner = new BaseIncreMode(couch_factory, video_helper, es_helper, docUploadHelper);

    }


    public void run(){
        logger.info("[VideoIncreMode] started!");
        int idx = 0;
        //int MAX_RUN = Integer.MAX_VALUE;
        while (this.incre_runner.hasNext()){
            incre_runner.runIncre( 50 );
            TransactionHelper.GCAndSleep(1000);
            idx += 1;
            logger.info("[VideoIncreMode]------------->>>>>>>>>>>>>> run times = {}\n\n", (idx));
        }
        logger.info("[VideoIncreMode] finished!");
    }


    /******************
     *
     * @param args
     */
    public static void main(String[] args){
        //SceneIncreMode scene_runner = new SceneIncreMode();
        //scene_runner.run();

        (new Thread(new VideoThread())).start();

    }


}
